package org.dtrd.modules.achievement.entity.po;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Getter;
import lombok.Setter;
import org.dtrd.modules.achievement.entity.bean.SigninTemplateContent;

import java.util.Arrays;
import java.util.Date;


/**
 * <p>
 * 打卡计划模板
 * </p>
 *
 * @author QR
 * @since 2022-01-04
 */
@Getter
@Setter
@TableName("dtrd_ent_rd_signin_template")
public class DtrdEntRdSigninTemplate {

    @TableId(value = "data_id", type = IdType.AUTO)
    private Integer dataId;

    /**
     * 模板名称
     */
    @TableField("sign_in_name")
    private String signInName;


    /**
     * 周一计划
     */
    @TableField("sign_in_json_monday")
    private String signInJsonMonday;

    /**
     * 周二计划
     */
    @TableField("sign_in_json_tuesday")
    private String signInJsonTuesday;

    /**
     * 周三计划
     */
    @TableField("sign_in_json_wednesday")
    private String signInJsonWednesday;

    /**
     * 周四计划
     */
    @TableField("sign_in_json_thursday")
    private String signInJsonThursday;

    /**
     * 周五计划
     */
    @TableField("sign_in_json_friday")
    private String signInJsonFriday;

    /**
     * 周六计划
     */
    @TableField("sign_in_json_saturday")
    private String signInJsonSaturday;

    /**
     * 周日计划
     */
    @TableField("sign_in_json_sunday")
    private String signInJsonSunday;

    @TableField("create_time")
    private Date createTime;

    @TableField("create_by")
    private String createBy;

    @TableField("update_time")
    private Date updateTime;

    @TableField("update_by")
    private String updateBy;

    @TableField("is_del")
    @TableLogic
    private Integer isDel;

    @TableField("org_code")
    private String orgCode;

    public DtrdEntRdSigninTemplate init() {
        createTime = new Date();
        isDel = 0;
        return this;
    }

    public DtrdEntRdSigninTemplate parseFromDTO(SigninTemplateContent dto) {
        dataId = dto.getDataId();
        signInName = dto.getSignInName();
        signInJsonMonday = deleteProperty(dto.getSignInJsonMonday());
        signInJsonTuesday = deleteProperty(dto.getSignInJsonTuesday());
        signInJsonWednesday = deleteProperty(dto.getSignInJsonWednesday());
        signInJsonThursday = deleteProperty(dto.getSignInJsonThursday());
        signInJsonFriday = deleteProperty(dto.getSignInJsonFriday());
        signInJsonSaturday = deleteProperty(dto.getSignInJsonSaturday());
        signInJsonSunday = deleteProperty(dto.getSignInJsonSunday());
        orgCode = dto.getOrgCode();
        return this;
    }

    @SuppressWarnings("all")
    public String deleteProperty(Object[] signinArray) {
        JSONArray jsonArray = new JSONArray(signinArray);
        for (Object obj : jsonArray) {
            JSONObject jsonObject = (JSONObject) obj;
            jsonObject.remove("item_id");
            jsonObject.remove("item");
        }
        return Arrays.toString(jsonArray.toArray());
    }

}
